﻿using DapperExtensions.ValueObject;
using HY.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DapperExtensions.Sql
{
    public interface ISqlDialect
    {
        DataBaseType DBType { get; }

        char OpenQuote { get; }
        char CloseQuote { get; }
        string BatchSeperator { get; }
        bool SupportsMultipleStatements { get; }
        char ParameterPrefix { get; }
        string EmptyExpression { get; }
        string GetTableName(string schemaName, string tableName, string alias, string dbName = "");
        string GetColumnName(string prefix, string columnName, string alias);
        string GetIdentitySql(string tableName);
        string GetPagingSql(string sql, int page, int resultsPerPage, IDictionary<string, object> parameters);
        string GetSetSql(string sql, int firstResult, int maxResults, IDictionary<string, object> parameters);
        bool IsQuoted(string value);
        string QuoteString(string value);

        string GetJoinString(JoinType joinType);

        string GetTopString(int pageIndex, int pageSize);
    }

    public abstract class SqlDialectBase : ISqlDialect
    {
        public DataBaseType DBType { get; protected set; }


        public virtual char OpenQuote
        {
            get { return '"'; }
        }

        public virtual char CloseQuote
        {
            get { return '"'; }
        }

        public virtual string BatchSeperator
        {
            get { return ";" + Environment.NewLine; }
        }

        public virtual bool SupportsMultipleStatements
        {
            get { return true; }
        }

        public virtual char ParameterPrefix
        {
            get
            {
                return '@';
            }
        }

        public string EmptyExpression
        {
            get
            {
                return "1=1";
            }
        }

        public virtual string GetTableName(string schemaName, string tableName, string alias, string dbName = "")
        {
            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new ArgumentNullException("TableName", "tableName cannot be null or empty.");
            }

            StringBuilder result = new StringBuilder();
            if (!string.IsNullOrWhiteSpace(schemaName))
            {
                if (!string.IsNullOrWhiteSpace(dbName))
                {
                    result.AppendFormat(QuoteString(dbName) + ".");
                }
                result.AppendFormat(QuoteString(schemaName) + ".");
            }
            result.AppendFormat(QuoteString(tableName));

            if (!string.IsNullOrWhiteSpace(alias))
            {
                result.AppendFormat(" AS {0}", QuoteString(alias));
            }
            return result.ToString();
        }

        public virtual string GetColumnName(string prefix, string columnName, string alias)
        {
            if (string.IsNullOrWhiteSpace(columnName))
            {
                throw new ArgumentNullException("ColumnName", "columnName cannot be null or empty.");
            }

            StringBuilder result = new StringBuilder();
            if (!string.IsNullOrWhiteSpace(prefix))
            {
                result.AppendFormat(QuoteString(prefix) + ".");
            }

            if (columnName != "*")
            {
                result.AppendFormat(QuoteString(columnName));
            }
            else
            {
                result.AppendFormat(columnName);
            }

            if (!string.IsNullOrWhiteSpace(alias))
            {
                result.AppendFormat(" AS {0}", QuoteString(alias));
            }

            return result.ToString();
        }

        public abstract string GetIdentitySql(string tableName);
        public abstract string GetPagingSql(string sql, int page, int resultsPerPage, IDictionary<string, object> parameters);
        public abstract string GetSetSql(string sql, int firstResult, int maxResults, IDictionary<string, object> parameters);

        public virtual bool IsQuoted(string value)
        {
            if (value.Trim()[0] == OpenQuote)
            {
                return value.Trim().Last() == CloseQuote;
            }

            return false;
        }

        public virtual string QuoteString(string value)
        {
            return IsQuoted(value) ? value : string.Format("{0}{1}{2}", OpenQuote, value.Trim(), CloseQuote);
        }

        public virtual string UnQuoteString(string value)
        {
            return IsQuoted(value) ? value.Substring(1, value.Length - 2) : value;
        }


        public string GetJoinString(JoinType joinType)
        {
            string joinString = string.Empty;
            switch (joinType)
            {
                case JoinType.InnerJoin:
                    joinString = "INNER JOIN";
                    break;
                case JoinType.LeftJoin:
                    joinString = "LEFT JOIN";
                    break;
                case JoinType.RightJoin:
                    joinString = "RIGHT JOIN";
                    break;
                case JoinType.CrossJoin:
                    joinString = "CROSS JOIN";
                    break;
                case JoinType.FullJoin:
                    joinString = "FULL JOIN";
                    break;
                default:
                    joinString = "INNER JOIN";
                    break;
            }
            return joinString;
        }


        public virtual string GetTopString(int pageIndex, int pageSize)
        {
            return string.Empty;    
        }
    }
}